package com.yb.web.member.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yb.base.web.BaseAction;
import com.yb.bussiness.member.SuggestService;
import com.yb.bussiness.member.SuggestServiceImpl;
import com.yb.domain.member.TpSuggest;
import com.yb.domain.member.TsSysuser;
import com.yb.util.comm.Contants;
import com.yb.util.comm.DateTimeUtil;
import com.yb.util.comm.RequestUtil;
import com.yb.util.framework.PageUtil;

/**
 * SuggestAction entity.
 * 
 * @author MyEclipse Persistence Tools
 */

public class SuggestAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private SuggestService service;
	private List rows;
	private int total;
	private TpSuggest suggest;
	private int flag;
	
	public SuggestAction() {
		service = new SuggestServiceImpl();
	}

	/**
	 * 后台检索
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String search() {
		menuCls = RequestUtil.getParam(request, "menuCls", "");
		Map<String, String> map = new HashMap<String, String>();
		int page = RequestUtil.getParam(request, "page", 1);
		int row = RequestUtil.getParam(request, "rows", 10);
		String title = RequestUtil.getParam(request, "title", "");
		String c_startTime = RequestUtil.getParam(request, "c_startTime", "");
		String c_endTime = RequestUtil.getParam(request, "c_endTime", "");
		String a_startTime = RequestUtil.getParam(request, "a_startTime", "");
		String a_endTime = RequestUtil.getParam(request, "a_endTime", "");
		map.put("title", title);
		map.put("c_startTime", c_startTime);
		map.put("c_endTime", c_endTime);
		map.put("a_startTime", a_startTime);
		map.put("a_endTime", a_endTime);
		PageUtil pu = service.getList(map, page, row);
		total = pu.getTotalRecords();
		rows = pu.getList();
		return Contants.JSONSTRING_RESULT_NAME;
	}
	/**
	 * 删除
	 * @return
	 */
	public String delete() {
		String idString = RequestUtil.getParam(request, "idString", "");
		boolean isFlag = false;
			isFlag = service.deleteByIdString(idString);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		if (isFlag)
			message = "删除成功";
		else
			message = "删除失败";
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");

		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {

			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 采纳
	 * @return
	 */
	public String onAutding() {
		String idString = RequestUtil.getParam(request, "idString", "");
		String type = RequestUtil.getParam(request, "type", "");
		boolean isFlag = false;
		isFlag = service.onAutding(idString,type);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		if (isFlag)
			message = "操作成功";
		else
			message = "操作失败";
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");
		
		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 
	 * @return
	 */
	public String suggestFlagChange() {
		String suggestFlag = RequestUtil.getParam(request, "suggestFlag", "");
		request.getSession().setAttribute("suggestFlag", suggestFlag);
		return null;
	}
	
	/**
	 * 保存建议
	 * @return
	 */
	public String addSuggest(){
		 TsSysuser tsSysuser = (TsSysuser) RequestUtil.getLoginUserFromSession(request, Contants.SESSION_USER);
          if(null!=tsSysuser){
        	  suggest.setSysuserId(Integer.valueOf(tsSysuser.getId().toString()));
        	  suggest.setSysuserName(tsSysuser.getUsername());
          }else{
        	  suggest.setSysuserId(0);
        	  suggest.setSysuserName("游客");  
          }
          try {
        	  suggest.setCreateTime(new Date());
        	  boolean isTrue = service.saveOrUpdate(suggest);
              if (isTrue)
                  response.getWriter().print("<script>parent.addResult_eva('1')</script>");
              else
                  response.getWriter().print("<script>parent.addResult_eva('2')</script>");
          }
          catch (Exception e) {
              e.printStackTrace();
              log.error("反馈意见保存失败", e);
              try {
                  response.getWriter().print("<script>parent.addResult_eva('2')</script>");
              }
              catch (IOException e1) {
                  e1.printStackTrace();
              }
              return null;
          }
          return null;
	}
	
    /**
     * 
     * 跳转建议信息页面
     */
    public String toSuggestInfo() {
        long id = RequestUtil.getParam(request, "id", 0l);
        suggest = service.read(id);
        suggest.setCreatTimeStr(DateTimeUtil.switchDateToString(suggest.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        suggest.setAcceptTimeStr(DateTimeUtil.switchDateToString(suggest.getAcceptTime(), "yyyy-MM-dd HH:mm:ss"));
        return "suggestinfo";
    }
    
    /**
     * 回复
     * 
     * @return
     */
    public String reply() {
    	String flag = RequestUtil.getParam(request, "flag", "");
        try {
//        	if("3".equals(flag))
//        		suggest.setCreateTime(new Date());
//        	else
//        		suggest.setAcceptTime(new Date());
            service.saveReply(suggest.getId(),suggest.getReplayContent());
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("suggest回复失败", e);
        }
//        if("3".equals(flag))
//        	return "yzPage";
        return "suggestmanage";
    }
    
	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}
	
	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public TpSuggest getSuggest() {
		return suggest;
	}

	public void setSuggest(TpSuggest suggest) {
		this.suggest = suggest;
	}
}